package com.lastproject.web.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;

import com.lastproject.dto.CategoryDTO;
import com.lastproject.dto.SubcategoryDTO;
import com.lastproject.dto.TopicDTO;
import com.lastproject.ejb.dao.CategoryDAORemote;
import com.lastproject.ejb.dao.SubcategoryDAORemote;
import com.lastproject.ejb.dao.TopicDAORemote;

public class TopicBean implements Serializable {

	/**
	 * 
//	 */
	private static final long serialVersionUID = 8760647518167325783L;

	@EJB
	TopicDAORemote topicDAORemote;

	@EJB
	CategoryDAORemote categoryDAORemote;

	@EJB
	SubcategoryDAORemote subcategoryDAORemote;

	CategoryDTO categoryDTO;

	SubcategoryDTO subcategoryDTO;

	TopicDTO topicDTO;

	private List<CategoryDTO> categories;

	private List<SubcategoryDTO> subcategories;

	private boolean alreadyCreated = false;

	public TopicBean() {

	}

	/**
	 * Create a new Topic
	 * 
	 * @return Returns a String containing [nextPage].xhtml
	 */
	public String createTopic() {
		alreadyCreated = false;

		List<TopicDTO> topicList = new ArrayList<TopicDTO>();
		topicList = topicDAORemote.findAll();

		for (TopicDTO t : topicList) {
			if (t.getIdCategory() == topicDTO.getIdCategory()) {
				if (t.getIdSubcategory() == topicDTO.getIdSubcategory()) {
					alreadyCreated = true;
					return "createTopic.xhtml";
				}
			}
		}

		topicDAORemote.create(topicDTO);
		return "surveyCreate.xhtml";
	}

	public CategoryDTO getCategoryDTO() {
		return categoryDTO;
	}

	public void setCategoryDTO(CategoryDTO categoryDTO) {
		this.categoryDTO = categoryDTO;
	}

	public SubcategoryDTO getSubcategoryDTO() {
		return subcategoryDTO;
	}

	public void setSubcategoryDTO(SubcategoryDTO subcategoryDTO) {
		this.subcategoryDTO = subcategoryDTO;
	}

	public TopicDTO getTopicDTO() {
		return topicDTO;
	}

	public void setTopicDTO(TopicDTO topicDTO) {
		this.topicDTO = topicDTO;
	}

	public List<CategoryDTO> getCategories() {
		return categoryDAORemote.findAll();
	}

	public void setCategories(List<CategoryDTO> categories) {
		this.categories = categories;
	}

	public List<SubcategoryDTO> getSubcategories() {
		return subcategoryDAORemote.findAll();
	}

	public void setSubcategories(List<SubcategoryDTO> subcategories) {
		this.subcategories = subcategories;
	}

	public boolean isAlreadyCreated() {
		return alreadyCreated;
	}

	public void setAlreadyCreated(boolean alreadyCreated) {
		this.alreadyCreated = alreadyCreated;
	}

}
